package com.dq.repository.account;

import com.dq.domain.service.ServiceProject;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.Account;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.WxUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author heshuaijun
 * @date 2019/9/1615:56
 */
public interface AccountRepository extends JpaRepository<Account, Long> {
    @Query(nativeQuery = true,value = "SELECT COALESCE(sum(a.price),0) price FROM `account` a  LEFT JOIN user_account_staff_user ua\n" +
            "on a.id = ua.account_id\n" +
            "where  ua.staff_user_id = ?1 and a.create_time >= ?2 and a.change_type = ?3 GROUP BY a.id")
    BigDecimal SumRecharge(Long staffUserId,Long time,Account.ChangeType changeType);

    List<Account> findByDeletedFalseAndUserAndCreateTimeGreaterThanAndCreateTimeLessThanAndChangeType(WxUser wxUser, Long beginTime , Long endTime, Account.ChangeType changeType);

    List<Account> findByDeletedFalseAndStaffUserAndCreateTimeGreaterThanAndCreateTimeLessThanAndAccountChangeType(BossUser staffUser, Long beginTime , Long endTime, Account.AccountChangeType accountChangeType);

    List<Account> findByDeletedFalseAndStaffUserAndCreateTimeGreaterThanAndCreateTimeLessThan(BossUser staffUser, Long beginTime , Long endTime);

    List<Account> findByDeletedFalseAndSysDeptAndCreateTimeGreaterThanAndCreateTimeLessThanAndAccountChangeTypeAndServiceProject(SysSection sysSection, Long beginTime , Long endTime, Account.AccountChangeType accountChangeType, ServiceProject serviceProject);


    @Query(nativeQuery = true, value = "SELECT count(a.id) FROM account a where  a.deleted = ?1 and a.create_time >= ?2 and a.create_time <= ?3 and a.staff_user_id = ?4 GROUP BY a.user_id")
    Long countByDeletedFalseAndCreateTimeGreaterThanAndCreateTimeLessThanAndStaffUserGroupByUserId(boolean deleted, Long beginTime , Long endTime, Long staffUserId);


    Account findByDeletedFalseAndOrderNo(String orderNo);
}
